package com.yanyeori.framework.datasource.builder;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Import;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * DataSource构造器工厂
 *
 * @author chenkuan 2021/4/16
 */
@Slf4j
@Import({HikariDataSourceBuilder.class, DruidDataSourceBuilder.class})
public class DataSourceBuilderFactory {

    private final Map<Class<? extends DataSource>, DataSourceBuilder> dataSourceBuilderMap = new HashMap<>();

    public DataSourceBuilderFactory(List<DataSourceBuilder> dataSourceBuilders) {
        dataSourceBuilders.forEach(x -> dataSourceBuilderMap.put(x.getType(), x));
        log.debug("Load datasource builders: [{}]", dataSourceBuilderMap.keySet().stream().map(Class::getSimpleName).collect(Collectors.joining(",")));
    }

    public DataSourceBuilder get(Class<? extends DataSource> clazz) {
        return dataSourceBuilderMap.get(clazz);
    }
}
